
class Food {

}
class Fruit extends Food {

}

class Apple extends Fruit {

}

class Banana extends Fruit {

}

class Plate<T> { // 设置泛型上限
    private T plate ;

    public T getPlate() {
        return plate;
    }

    public void setPlate(T plate) {
        this.plate = plate;
    }
}

public class TestDemo {


    public static void fun(Plate<? super Fruit> temp){
        temp.setPlate(new Apple());
        temp.setPlate(new Banana());
        temp.setPlate(new Fruit());

        //Fruit fruit = temp.getPlate();
    }
    public static void main(String[] args) {
        // 可以看到p1 与 p2 调用 fun 方法后都是正确的
        Plate<Fruit> plate1 = new Plate<>();
        plate1.setPlate(new Fruit());
        fun(plate1);

        Plate<Food> plate2 = new Plate<>();
        plate2.setPlate(new Food());
        fun(plate2);
    }

    public static void main1(String[] args) {
        // 可以看到p1 与 p2 调用 fun 方法后都是正确的
        Plate<Apple> plate1 = new Plate<>();
        plate1.setPlate(new Apple());
        fun1(plate1);

        Plate<Banana> plate2 = new Plate<>();
        plate2.setPlate(new Banana());
        fun1(plate2);
    }

    // 此时继承的是 Fruit 类，表名 fun 方法可以调用 Fruit、Apple or Banana
    public static void fun1(Plate<? extends Fruit> temp){
//        temp.setPlate(new Apple());
//        temp.setPlate(new Banana());
//        temp.setPlate(new Fruit());
     Fruit fruit = temp.getPlate();

     //Apple apple = temp.getPlate();
     //Banana banana = temp.getPlate();
    }
}
